<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use Hyperf\Guzzle\ClientFactory;
use Hyperf\HttpServer\Contract\RequestInterface;

class IndexController extends AbstractController
{
    /**
     * @var ClientFactory
     */
    private $clientFactory;

    public function __construct(ClientFactory $clientFactory)
    {
        $this->clientFactory = $clientFactory;
    }

    public function index(RequestInterface $request)
    {
        if ($request->input('key') != env('API_KEY')) {
            return;
        }

        $name = $request->input('user_name');
        if (! $name) {
            return;
        }

        $repository = $request->input('repository');
        $after = $request->input('after', '');
        $before = $request->input('before', '');
        $msg = [
            'msgtype' => 'text',
            'text' => [
                'content' => $name . '推送了' . $repository['name'] . ', before:'. $before .', after:' . $after
            ],
        ];

        $response = $this->clientFactory->create()->post(
            env('COMPANY_WECHAT_WEBHOOK_URL'),
            [
                'headers' => ['Content-Type' => 'application/json;utf-8'],
                'json' => $msg,
            ]
        );

        stdLog()->info($response->getBody());

        stdLog()->info($name . '提交了代码');
    }
}
